

//激活摘要栏目的自动完成
function enableVoucherSummaryAutoComplete(source) {

    $('input[id*=voucher_summary]').autocomplete({
        //source: availableSummaryTitle,
        source: source,
        delay: 1,
        minLength:0
    });

}

//激活科目栏位的自动完成
function enableVoucherSubjectAutoComplete(source) {

    $('input[id*=voucher_subject]').autocomplete({
        source: source,
        delay: 1,
        minLength:0
    });

}

//取所有的credit栏位的值
function getCredit() {

    var credit = new Array()
    $("input[id*=voucher_credit]").each(function(index) {
        if($(this).val() == '') {
            credit[index] = 0.00
        } else {
            credit[index] = parseFloat($(this).val())
        }
         
    })
    return credit;
}

//取所有debit栏位的值
function getDebit() {

    var debit = new Array()
    $("input[id*=voucher_debit]").each(function(index) {
        if($(this).val() == '') {
            debit[index] = 0.00
        } else {
            debit[index] = parseFloat($(this).val())
        }
         
    })
    return debit;
}

//取摘要栏位
function getSummary() {
    var summary = new Array()
    $("input[id*=voucher_summary]").each(function(index){
        summary[index] = $(this).val()
    })
    return summary
}

function getSubject() {
    var subject = new Array()
    //$("input[id*=voucher_subject]").each(function(index){ //将科目栏位改成了select选单
    $("select[id*=voucher_subject]").each(function(index){
        subject[index] = $(this).val()
    })

    return subject
}

//对debit和credit的值进行汇总
function summarizeDebitCredit(debit, credit) {

    var debit_sum = 0;
    var credit_sum = 0;
    for(var i = 0; i< debit.length; i++) {
        debit_sum += debit[i]
    }

    for(i=0; i < credit.length; i++) {
        credit_sum += credit[i]
    }

    //return new Array(Math.round(debit_sum*100)/100, Math.round(credit_sum)*100/100)
    //javascript 1.5
    //toFixed(). toPrecision()
    return new Array(debit_sum.toFixed(2), credit_sum.toFixed(2))
}

//检查debit和credit栏位输入是否正确
function validateDebitCreditInput(debit, credit) {

    var error = 0;
    for(var i = 0; i<debit.length; i++) {
        if( (debit[i] != '' || debit[i] != 0 )  && (credit[i] != '' || credit[i] != 0 ) ) {
            error += 1;
        //alert("第" + (i+1) +  "行记录输入有错误，请修正后重试.");
        }
    }
    if(error > 0 ) {
        return false
    } else {
        return true
    }
}


//检查debit和credit平衡
function validateDebitCreditBalance(debit,credit) {

    var debit_sum = 0
    var credit_sum = 0
    for(i = 0; i< debit.length; i++) {
        debit_sum += debit[i]
    }

    for(i=0; i < credit.length; i++) {
        credit_sum += credit[i]
    }

    if(debit_sum == credit_sum) {
        return true;
    } else {
        return false;
    }
}

//检查摘要栏位是否填写正确
function validateVoucherFormInput(summary, subject, debit, credit) {

    //1.摘要栏位为必填项
    //2.允许有空行的出现，即摘要，科目和借贷金额都为空
    var error = 0
    for(var i = 0; i < summary.length; i++) {
        //如果该行是空行，跳过
        if(summary[i] == '' && debit[i] == 0 && credit[i] == 0 && subject[i] == '') {
            continue
        }
        //如果只填了summary，但借贷双方都为空或是科目为空，错误
        if( summary[i] != '' &&  ( (debit[i] == 0 && credit[i] == 0) || subject[i] == '' ) ) {
            error += 1
        }
        //摘要为空，其它栏位有填写东西，则出错
        if( summary[i] == '' && (subject[i] != '' || debit[i] != 0 || credit[i] != 0 ) ) {
            error += 1
        }
        //摘要和科目不为空，但是借贷栏位未填金额则出错
        if(summary[i] != '' && subject[i] != '' &&  debit[i] == 0 && credit[i] == 0 ) {
            error += 1
        }
        if(error) {
            return false
        }
    }
    /*
    if(error) {
        return false
    } else {
        return true
    }*/
    return true
}
